package com.yss.sofa.ide.builder.dbscript.ui.wizard;

import java.io.File;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import com.yss.sofa.common.jdbc.DBType;
import com.yss.sofa.ide.builder.dbscript.Global;
import com.yss.sofa.ide.builder.dbscript.model.ScriptInfo;
import com.yss.sofa.ide.builder.dbscript.service.DBTypeMapService;
import com.yss.sofa.ide.builder.dbscript.ui.composite.GlobalTypeMapping;
import com.yss.sofa.ide.builder.dbscript.vo.ColumnTypeMap;
import com.yss.sofa.ide.builder.dbscript.vo.DBTypeMap;
import com.yss.sofa.ide.config.util.ResourceUtil;
import com.yss.sofa.ide.ui.wizard.BaseWizardPage;

/**
 * @author huangsq
 * @version 1.0, 2013-11-15
 * @since 1.0, 2013-11-15
 */
public class GlobalTypeMappingPage extends BaseWizardPage {

  public static final String ID = "GlobalTypeMappingPage";
  private ScriptInfo info;
  private DBTypeMap map;

  /**
   * Create the wizard.
   */
  public GlobalTypeMappingPage(ScriptInfo info) {
    super(ID);
    setTitle(Messages.GlobalTypeMappingPage_TITLE);
    setDescription(Messages.GlobalTypeMappingPage_DESC);
    this.info = info;
    this.map = getDefaultOrUserMap(new DBTypeMapService(), getMapPath());
    setPageComplete(true);
  }

  private String getMapPath() {
    return this.info.getProjectName() == null ? Global.getMappingFile() : getUserMappingFile();
  }

  private DBTypeMap getDefaultOrUserMap(DBTypeMapService dbTypeMapService, String dbTypeMapPath) {
    return dbTypeMapService.getDBTypeMap(dbTypeMapPath);
  }

  /**
   * Create contents of the wizard.
   * @param parent
   */
  @Override
  public void createControl(Composite parent) {
    IWizardPage page = getPreviousPage().getPreviousPage();
    NewDBManagerProjectWizardPage projectPage = (NewDBManagerProjectWizardPage) page;
    String sourceName = projectPage.getSourceNames();
    String[] targetNames = projectPage.getTargetNames();
    Composite container = new Composite(parent, SWT.NULL);

    container.setLayout(new GridLayout(1, false));

    TabFolder tabFolder = new TabFolder(container, SWT.NONE);
    tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
    for (int i = 0; i < targetNames.length; i++) {
      TabItem tbtmOracledb = new TabItem(tabFolder, SWT.NONE);
      tbtmOracledb.setText(sourceName + Messages.GlobalTypeMappingPage_RIGHT_ARROW + targetNames[i]);
      GlobalTypeMapping globalTypeMapping = new GlobalTypeMapping(tabFolder, SWT.NONE, getMapping(sourceName,
              targetNames[i]));
      tbtmOracledb.setControl(globalTypeMapping);
    }
    setControl(container);
  }

  private ColumnTypeMap getMapping(String sourceName, String targetName) {
    ColumnTypeMap columnTypeMap = this.map.getColumnTypeMap(DBType.getEnumType(sourceName),
            DBType.getEnumType(targetName));
    return columnTypeMap;
  }

  public String getUserMappingFile() {
    String path = Platform.getInstanceLocation().getURL().getPath();
    if (path.startsWith(Messages.GlobalTypeMappingPage_SEPARATOR)) {
      path = path.substring(1) + this.info.getProjectName() + Messages.GlobalTypeMappingPage_USER_DB_MAPPING_FILE;
    }
    return new File(path).exists() ? path : Global.getMappingFile();
  }

  @Override
  public void dispose() {
    super.dispose();
    setControl(null);
  }

  /**
   * @return the map
   */
  public DBTypeMap getMap() {
    return this.map;
  }

}
